package com.caigou.modules.forum.repository

import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.bean.HttpResult
import com.caigou.comm.base.bean.PrizeBallBean
import com.caigou.modules.forum.bean.*
import com.caigou.modules.forum.network.HttpForumService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/09
 */
@ActivityRetainedScoped
class ForumRepository @Inject constructor() {

    fun getForumList(page: Int, limit: Int, type: Int, sort: Int, currentUserId: String): Flow<HttpResult<PostBean>> {
        return HttpForumService.service.getForumList(page, limit, type, sort, currentUserId)
    }

    fun getMarketList(page: Int, limit: Int, superMaterialType: Int, currentUserId: String): Flow<HttpResult<PostBean>> {
        return HttpForumService.service.getMarketList(page, limit, 6, superMaterialType, currentUserId)
    }

    fun getHistoryPostList(page: Int, limit: Int, type: Int, userId: String): Flow<HttpResult<PostBean>> {
        return HttpForumService.service.getHistoryPostList(page, limit, type, userId)
    }

    fun getPostDetail(dataId: Int, currentUserId: String): Flow<HttpResult<PostDetailBean>> {
        return HttpForumService.service.getPostDetail(dataId, currentUserId)
    }

    fun getCommentList(page: Int, limit: Int, dataId: Int, sort: Int, currentUserId: String): Flow<HttpResult<CommentBean>> {
        return HttpForumService.service.getCommentList(page, limit, dataId, sort, currentUserId)
    }

    fun getCommentDetail(dataId: Int, replyId: Int, userId: String): Flow<HttpResult<CommentItemBean>> {
        return HttpForumService.service.getCommentDetail(dataId, replyId, userId)
    }

    fun setPostLike(dataId: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dataId", dataId)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpForumService.service.setPostLike(body)
    }

    fun setCommentLike(replyId: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("replyId", replyId)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpForumService.service.setCommentLike(body)
    }

    fun setPostReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.setReply(body.build())
    }

    fun setCommentReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, parentId: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
            .addFormDataPart("parentId", parentId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.setReply(body.build())
    }

    fun publishPost(
        files: MutableList<File>, title: String, content: String,
        userId: String, dataType: Int,
        redPacketMoney: Float, redPacketCount: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("title", title)
            .addFormDataPart("content", content)
            .addFormDataPart("userId", userId)
            .addFormDataPart("dataType", dataType.toString())
        if (redPacketMoney != 0.00f && redPacketCount != 0) {
            body.addFormDataPart("redPacketMoney", redPacketMoney.toString())
                .addFormDataPart("redPacketCount", redPacketCount.toString())
        }
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.publishPost(body.build())
    }

    fun publishContestPost(
        files: MutableList<File>, title: String, content: String,
        userId: String, dataType: Int, dareContent: String, dareType: Int,
        redPacketMoney: Float, redPacketCount: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("title", title)
            .addFormDataPart("content", content)
            .addFormDataPart("userId", userId)
            .addFormDataPart("dataType", dataType.toString())
            .addFormDataPart("dareContent", dareContent)
            .addFormDataPart("dareType", dareType.toString())
        if (redPacketMoney != 0.00f && redPacketCount != 0) {
            body.addFormDataPart("redPacketMoney", redPacketMoney.toString())
                .addFormDataPart("redPacketCount", redPacketCount.toString())
        }
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.publishPost(body.build())
    }

    fun publishFifaPost(
        files: MutableList<File>, title: String, content: String,
        userId: String, dataType: Int, fifaType: Int, redPacketMoney: Float, redPacketCount: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("title", title)
            .addFormDataPart("content", content)
            .addFormDataPart("userId", userId)
            .addFormDataPart("dataType", dataType.toString())
            .addFormDataPart("fifaType", fifaType.toString())
        if (redPacketMoney != 0.00f && redPacketCount != 0) {
            body.addFormDataPart("redPacketMoney", redPacketMoney.toString())
                .addFormDataPart("redPacketCount", redPacketCount.toString())
        }
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.publishPost(body.build())
    }

    fun publishMarketPost(
        files: MutableList<File>, title: String,
        content: String, userId: String,
        dataType: Int, superMaterialType: Int,
        redPacketMoney: Float, redPacketCount: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("title", title)
            .addFormDataPart("content", content)
            .addFormDataPart("userId", userId)
            .addFormDataPart("dataType", dataType.toString())
            .addFormDataPart("superMaterialType", superMaterialType.toString())
        if (redPacketMoney != 0.00f && redPacketCount != 0) {
            body.addFormDataPart("redPacketMoney", redPacketMoney.toString())
                .addFormDataPart("redPacketCount", redPacketCount.toString())
        }
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.publishPost(body.build())
    }

    fun getPrizeBallList(): Flow<HttpResult<MutableList<PrizeBallBean>>> {
        return HttpForumService.service.getPrizeBallList()
    }

    fun getFifaTypeList(): Flow<HttpResult<MutableList<FifaItemBean>>> {
        return HttpForumService.service.getFifaTypeList()
    }

    fun searchUser(limit: Int, page: Int, searchType: Int, searchText: String): Flow<HttpResult<SearchUserBean>> {
        return HttpForumService.service.searchUser(limit, page, searchType, searchText)
    }

    fun searchPost(limit: Int, page: Int, searchType: Int, searchText: String): Flow<HttpResult<PostBean>> {
        return HttpForumService.service.searchPost(limit, page, searchType, searchText)
    }

    fun getAdList(): Flow<HttpResult<MutableList<AdItemBean>>> {
        return HttpForumService.service.getAdList()
    }

    fun getNoticeList(): Flow<HttpResult<MutableList<NoticeItemBean>>> {
        return HttpForumService.service.getNoticeList()
    }

    fun getNoticeDetail(id: Int, userId: String): Flow<HttpResult<NoticeItemBean>> {
        return HttpForumService.service.getNoticeDetail(id, userId)
    }


    fun getNoticeCommentList(page: Int, limit: Int, dataId: Int, sort: Int, currentUserId: String): Flow<HttpResult<CommentBean>> {
        return HttpForumService.service.getNoticeCommentList(page, limit, dataId, sort, currentUserId)
    }

    fun getNoticeCommentDetail(noticeId: Int, replyId: Int, userId: String): Flow<HttpResult<CommentItemBean>> {
        return HttpForumService.service.getNoticeCommentDetail(noticeId, replyId, userId)
    }

    fun setNoticeCommentReply(
        content: String,
        files: MutableList<File>,
        senderId: String,
        receiverId: String,
        noticeId: Int,
        parentId: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("noticeId", noticeId.toString())
            .addFormDataPart("parentId", parentId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.setNoticeReply(body.build())
    }

    fun setNoticeReply(content: String, files: MutableList<File>, senderId: String, noticeId: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("noticeId", noticeId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpForumService.service.setNoticeReply(body.build())
    }

    fun setNoticeLike(noticeId: Int, senderId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("noticeId", noticeId)
        jsonObject.put("senderId", senderId)
        val body = jsonObject.toString().toRequestBody()
        return HttpForumService.service.setNoticeLike(body)
    }

    fun setNoticeCommentLike(replyId: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("replyId", replyId)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpForumService.service.setNoticeCommentLike(body)
    }

    fun getPacketRecordList(page: Int, limit: Int, redPacketId: Int): Flow<HttpResult<PacketRecordBean>> {
        return HttpForumService.service.getPacketRecordList(page, limit, redPacketId)
    }

    fun getPacketRecordList2(redPacketId: Int): Flow<HttpResult<PacketRecordBean>> {
        return HttpForumService.service.getPacketRecordList2(redPacketId)
    }

    fun getPacketDetail(redPacketId: Int, userId: String): Flow<HttpResult<PacketDetailBean>> {
        return HttpForumService.service.getPacketDetail(redPacketId, userId)
    }

    fun getGiftRankList(userId: String, currentUserId: String): Flow<HttpResult<MutableList<PostRankItemBean>>> {
        return HttpForumService.service.getGiftRankList(userId, currentUserId)
    }
}